<?php
namespace App\Controllers\Sys;

use App\Controllers\BaseController;
use App\Models\Project;
use App\Models\ProjectTempLogFile;
use App\Library\Helper;

class ProjectController extends BaseController {

    public function initialize() {
        parent::initialize();
    }

    //----------------------------------------------------------------
    // 会员营销项目列表
    //----------------------------------------------------------------
    public function indexAction() {
        $this->display('sys/project/index');
    }

    //----------------------------------------------------------------
    // 获取会员营销项目信息
    //----------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page') != '' ? $this->request->getPost('page') : $this->request->getQuery('page'),
            'size' => $this->request->getPost('size') != '' ? $this->request->getPost('size') : $this->request->getQuery('size')
        ];

        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页显示的条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset = intval(($data['page'] - 1) * $data['size']);

        $menu = new Project();
        $ret  = $menu->gets($offset, $data['size']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        }
        return $this->sendAjax(false, "获取数据失败，系统错误！");
    }

    //----------------------------------------------------------------
    // 获取会员营销项目信息总数
    //----------------------------------------------------------------
    public function countAction() {
        $project = new Project();
        $ret     = $project->total();
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }

    //----------------------------------------------------------------
    // 新增会员营销项目信息
    //----------------------------------------------------------------
    public function addAction() {
        if($this->request->isGet()) {
            $this->display('/sys/project/add');
        } else {
            $data = [
                'name'           => trim($this->request->getPost('name')),
                'info_type'      => $this->request->getPost('info_type'),
                'total'          => $this->request->getPost('total'),
                'fail'           => $this->request->getPost('fail'),
                'rate'           => $this->request->getPost('rate'),
                'each_length'    => $this->request->getPost('each_length'),
                'each_fee_count' => $this->request->getPost('each_fee_count'),
                'fee_count'      => $this->request->getPost('fee_count'),
                'status'         => $this->request->getPost('status'),
            ];
            if($data['name'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，项目名称不能为空！');
            }
            if($data['info_type'] == '' || !is_numeric($data['info_type'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，信息类型必须选择！');
            }
            if($data['total'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，总执行条数不能为空！');
            }
            if(!is_numeric($data['total'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，总执行条数必须是一个数字！');
            }
            if($data['fail'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，失败总数不能为空！');
            }
            if(!is_numeric($data['fail'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，失败总数必须是一个数字！');
            }
            if($data['rate'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，成功率不能为空！');
            }
            if(!is_numeric($data['rate'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，成功率必须是一个数字！');
            }
            if($data['each_length'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，单条短信长度不能为空！');
            }
            if(!is_numeric($data['each_length'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，单条短信长度必须是一个数字！');
            }
            if($data['each_fee_count'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，单条计费条数不能为空！');
            }
            if(!is_numeric($data['each_fee_count'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，单条计费条数必须是一个数字！');
            }
            if($data['fee_count'] == '') {
                return $this->sendAjax(false, '新增会员营销项目失败，总的计费条数不能为空！');
            }
            if(!is_numeric($data['fee_count'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，总的计费条数必须是一个数字！');
            }
            if($data['status'] == '' || !is_numeric($data['status'])) {
                return $this->sendAjax(false, '新增会员营销项目失败，状态值必须选择！');
            }
            $data['run_at']         = Helper::getNow();
            $data['create_at']      = Helper::getNow();
            $data['total']          = intval($data['total']);
            $data['fail']           = intval($data['fail']);
            $data['each_length']    = intval($data['each_length']);
            $data['each_fee_count'] = intval($data['each_fee_count']);
            $data['fee_count']      = intval($data['fee_count']);
            $project                = new Project();
            $data                   = $project->add($data);
            if($data) {
                return $this->sendAjax(true, '新增会员营销项目成功！');
            } else {
                return $this->sendAjax(false, '新增会员营销项目失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 编辑会员营销项目信息
    //----------------------------------------------------------------
    public function editAction() {
        if($this->request->isGet()) {
            $this->display('/sys/project/edit');
        } else {
            $data = [
                'id'             => intval($this->request->getPost('id')),
                'name'           => trim($this->request->getPost('name')),
                'info_type'      => $this->request->getPost('info_type'),
                'total'          => $this->request->getPost('total'),
                'fail'           => $this->request->getPost('fail'),
                'rate'           => $this->request->getPost('rate'),
                'each_length'    => $this->request->getPost('each_length'),
                'each_fee_count' => $this->request->getPost('each_fee_count'),
                'fee_count'      => $this->request->getPost('fee_count'),
                'status'         => $this->request->getPost('status'),
            ];
            if($data['name'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，项目名称不能为空！');
            }
            if($data['info_type'] == '' || !is_numeric($data['info_type'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，信息类型必须选择！');
            }
            if($data['total'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，总执行条数不能为空！');
            }
            if(!is_numeric($data['total'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，总执行条数必须是一个数字！');
            }
            if($data['fail'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，失败总数不能为空！');
            }
            if(!is_numeric($data['fail'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，失败总数必须是一个数字！');
            }
            if($data['rate'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，成功率不能为空！');
            }
            if(!is_numeric($data['rate'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，成功率必须是一个数字！');
            }
            if($data['each_length'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，单条短信长度不能为空！');
            }
            if(!is_numeric($data['each_length'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，单条短信长度必须是一个数字！');
            }
            if($data['each_fee_count'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，单条计费条数不能为空！');
            }
            if(!is_numeric($data['each_fee_count'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，单条计费条数必须是一个数字！');
            }
            if($data['fee_count'] == '') {
                return $this->sendAjax(false, '编辑会员营销项目失败，总的计费条数不能为空！');
            }
            if(!is_numeric($data['fee_count'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，总的计费条数必须是一个数字！');
            }
            if($data['status'] == '' || !is_numeric($data['status'])) {
                return $this->sendAjax(false, '编辑会员营销项目失败，状态值必须选择！');
            }
            $data['total']          = intval($data['total']);
            $data['fail']           = intval($data['fail']);
            $data['each_length']    = intval($data['each_length']);
            $data['each_fee_count'] = intval($data['each_fee_count']);
            $data['fee_count']      = intval($data['fee_count']);
            $project                = new Project();
            $data                   = $project->edit($data);
            if($data) {
                return $this->sendAjax(true, '编辑会员营销项目成功！');
            } else {
                return $this->sendAjax(false, '编辑会员营销项目失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 获取某个具体的会员营销项目信息
    //----------------------------------------------------------------
    public function getAction() {
        $id      = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
        $project = new Project();
        $entity  = $project->get($id);
        if($entity !== false) {
            return $this->sendAjax(true, $entity);
        }
        return $this->sendAjax(false, '获取数据失败，系统错误！');
    }

    //----------------------------------------------------------------
    // 删除某个具体的会员营销项目信息
    //----------------------------------------------------------------
    public function removeAction() {
        $id      = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
        $project = new Project();
        if($project->remove($id)) {
            return $this->sendAjax(true, '删除成功');
        } else {
            return $this->sendAjax(false, "删除失败，系统错误！");
        }
    }

    //----------------------------------------------------------------
    // 编辑会员营销项目信息
    //----------------------------------------------------------------
    public function logmangerAction() {
        if($this->request->isGet()) {
            $this->display('/sys/project/detail');
        } else {
        }
    }


    //----------------------------------------------------------------
    // 获取某个具体的会员营销项目信息
    //----------------------------------------------------------------
    public function logAction() {
        if($this->request->isGet()) {
            $this->display('/sys/project/log');
        } else {
            $params = $this->dispatcher->getParams();
            $param  = 'get';
            if($params != '' && is_array($params) && isset($params[0]) && in_array($params[0], ['get', 'submit'])) {
                $param = $params[0];
            }
            if($param == 'get') {
                $projectId = $this->request->getPost('project_id') != '' ? $this->request->getPost('project_id') : $this->request->getQuery('project_id');
                $logFile   = new ProjectTempLogFile();
                $entity    = $logFile->get($projectId);
                if($entity !== false) {
                    return $this->sendAjax(true, $entity);
                }
                return $this->sendAjax(false, '获取数据失败，系统错误，请稍后再试！');
            }
            if($param == 'submit') {
                $data    = [
                    'id'         => intval($this->request->getPost('id')),
                    'project_id' => trim($this->request->getPost('project_id')),
                    'file'       => $this->request->getPost('file'),
                    'status'     => $this->request->getPost('status')
                ];
                $project = new ProjectTempLogFile();
                if($data['id'] > 0) {
                    unset($data['run_at']);
                    unset($data['create_at']);
                    $ret = $project->edit($data);
                } else {
                    unset($data['id']);
                    $data['create_at'] = Helper::getNow();

                    $ret = $project->add($data);
                }
                if($ret) {
                    return $this->sendAjax(true, '编辑会员营销项目成功！');
                } else {
                    return $this->sendAjax(false, '编辑会员营销项目失败，系统错误！');
                }
            }
        }
    }
}
